#include "DominoManager.h"
#include <algorithm>
#include <stdexcept>
#include <string>

namespace dgame
{

typedef std::vector<Domino>::iterator dIter;
typedef std::vector<Domino>::const_iterator cdIter;

DominoManager::DominoManager(const DominoManager &dm)
{
    *this = dm;
}

void DominoManager::addDomino(const Domino &d)
{
    mDomino.push_back(d);
}

bool DominoManager::checkDomino(const Domino &d) const
{
    cdIter tmp = std::find(mDomino.begin(), mDomino.end(), d);
    if ( tmp != mDomino.end())
        return true;
    else
        return false;
}

void DominoManager::deleteDomino(const Domino &d)
{
    dIter tmp = std::find(mDomino.begin(), mDomino.end(), d);
    if (tmp != mDomino.end())
        mDomino.erase(tmp);
    else
        throw DominoNotFoundedException();
}

const Domino &DominoManager::operator[](uint n) const
{
    try
    {
         return mDomino.at(n);
    }
    catch (std::out_of_range &ex)
    {
        throw OutOfRangeException(std::string("DominoManager::") + ex.what());
    }
}

const DominoManager &DominoManager::operator=(const DominoManager &dm)
{
    this->mDomino = dm.mDomino;
    return (*this);
}

void DominoManager::shuffleDomino()
{
	std::random_shuffle(mDomino.begin(), mDomino.end());
}
}
